'use strict';

import Base from './base.js';

export default class extends Base {

  * indexAction () {
    yield *this.initData();
    let model = this.model('Academy');
    let jssdk = yield *this.getJsSdk();
    this.assign('data', yield model.select());
    this.assign('jsSdk', jssdk);
    return this.display();
  }
  * detailAction () {
    let id = this.get('id');
    let AcademyModel = this.model('Academy');
    let Pic = this.model('Image');
    let data = yield AcademyModel.where({
      Aid: id
    }).find();
    let pic = yield Pic.where({
      Iaid: id
    }).select();
    console.log(pic);
    this.assign('data', data);
    this.assign('pic', pic);
    return this.display();
  }
  * voteAction () {
    let stu = yield this.session('stu');
    let openid = yield this.session('openid');
    let carexbs = yield this.session('carexbs');
    return;
    if (!stu || !carexbs) {
      return this.end(JSON.stringify({
        status: 400,
        info: 'fail'
      }));
    } else {
      let User = this.model('User'),
      Academy = this.model('Academy');

      let openid = yield this.session('openid'),
      Aid = this.post('Aid'),//拉拉队id数组
      [id1, id2, id3, id4, id5] = Aid.split(',');

      let lastVote = (yield User.where({
          Uopenid: openid
      }).find()).Ulastvote;
      if (copDate(lastVote)) {
        yield User
          .where({
            Uopenid: openid
          })
          .update({
            Ulastvote: new Date().getTime()
          });
        yield Academy
          .where(`Aid = ${id1} OR Aid = ${id2} OR Aid = ${id3} OR Aid = ${id4} OR Aid = ${id5}`)
          .increment('Avote', 1);
        return this.end(JSON.stringify({
          status: 200,
          info: 'success'
        }));
      } else {
        return this.end(JSON.stringify({
          status: 200,
          info: 'fail'
        }));
      }
    }
  }
}
function copDate (oldTime) {
  let time = new Date().getTime();
  return Math.floor(time / 86400000) > Math.floor(oldTime / 86400000);
}
